<template>
	<view class="page">
		<image src="../../static/login.png" mode="widthFix"></image>
		<div class="input">
			<button @click="getUserProfile" v-if="!userInfo">微信授权登录</button>
			<button @click="submit" v-else open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">授权手机号码</button>
			<button class="back" @click="goIndex">取消</button>
		</div>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: "",
				session: "",
				openid: "",
				code: "",
			}
		},
		methods: {
			goIndex(){
				uni.reLaunch({
					url: '/pages/shop/list'
				})
			},
			onGetPhoneNumber(e) {
				console.log(e)
				// uni.showLoading({
				// 	title: '请等待',
				// 	mask: false
				// })
				const encryptedData = e.detail.encryptedData
				const iv = e.detail.iv
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					this.login(encryptedData, iv)
				} else {
					uni.showToast({
						title: '请重新获取',
						icon: "none"
					})
				}
			},
			login(encryptedData, iv) {
				console.log()
				this.$get('user/bindmobile', {
					session: this.session,
					iv: iv,
					encryptedData: encryptedData,
					nickname: this.userInfo.nickName,
					platform: "wechat",
					apptype: 2,
					avatar: this.userInfo.avatarUrl,
					openid: this.openid,
				}, (res) => {
					this.noCode = false;
					var token = res.userinfo.token;
					uni.setStorageSync('token', token);
					if(this.fx_id){this.fx()}
					uni.navigateBack({
					  success: () => {
					    let page = getCurrentPages().pop();
					    if (page) {
					       page.onLoad(page.options);//执行上个页面的方法
					    };
					  }
					})
					// this.getUserInfo()
				})
			},
			getUserProfile() {
				uni.getUserProfile({
					desc: "用于完善个人信息",
					success: (res) => {
						let userInfo = res.userInfo;
						this.userInfo = userInfo;
						uni.setStorageSync('userInfo', userInfo);
						this.register(this.code)
					},
					fail: (res) => {
						console.log(res)
					}
				})
			},
			register(authCode) {
				this.$get('user/third', {
					code: authCode,
					nickname: this.userInfo.nickName,
					platform: "wechat",
					apptype: 2
				}, (res) => {
					if (res.userinfo) {
						this.noCode = false;
						uni.setStorageSync('token', res.userinfo.token);
						if(this.fx_id){this.fx()}
						uni.navigateBack({
						  success: () => {
						    let page = getCurrentPages().pop();
						    if (page) {
						       page.onLoad(page.options);//执行上个页面的方法
						    };
						  }
						})
						return
					}
					this.openid = res.thirdinfo.openid
					this.session = res.thirdinfo.access_token;

					console.log(res)
					// this.getUserInfo()
				})
			},
			back(){
				uni.navigateBack()
			},
			fx(){
				this.$post('user/fx', {fx_id:this.fx_id}, (res) => {
					
				})
			}
		},
		onLoad(options) {
			uni.login({
				provider: 'weixin',
				success: (loginRes) => {
					this.code = loginRes.code;
					console.log(loginRes); // 获取用户信息 

				}
			});
			this.fx_id = uni.getStorageSync('scene')
		},
		onShow() {}
	}
</script>

<style>
	.page {
		background-color: #FFFFFF;
		padding-top: 160rpx;
		height: 100vh;
	}

	image {
		width: 210rpx;
		height: auto;
		display: block;
		margin: 0 auto;
	}

	.input {
		margin: 0 auto;
		width: 630rpx;
		margin-top: 80rpx;
	}

	.input button {
		color: #FFFFFF;
		font-size: 28rpx;
		text-align: center;
		height: 80rpx;
		line-height: 80rpx;
		background: #FE0520;
		border-radius: 40rpx;
	}
	.input .back{
		background-color: #FFFFFF;
		color: #999;
		border:1px solid #999;
		margin-top: 30rpx;
	}
</style>
